<template>
  <div style="height: 100vh; display: flex; align-items: center; justify-content: center; background-color: #42b983">
    <div style="display: flex; background-color: white; width:60%; border-radius: 10px; overflow: hidden">
      <div style="flex: 1">
        <img src="../assets/img/login.jpg" alt="" style="width: 100%; height: 100%;">
      </div>
      <div style="flex: 1; display: flex; align-items: center; justify-content: center">
        <el-form :model="user" :rules="rules" ref="loginRef">
          <div style="font-size: 30px; font-weight: bold; text-align: center; margin-bottom: 25px;">产品认证管理系统
          </div>

          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user-solid" placeholder="请输入用户名"
                      v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" show-password placeholder="请输入密码"
                      v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item prop="input_valid">
            <div style="display: flex">
              <el-input prefix-icon="el-icon-success" size="medium" placeholder="验证码" style="flex: 1"
                        v-model="user.input_valid"></el-input>
              <div style="flex: 1; width: 20px; height: 20px;margin-top: 4px ">
                <valid-code @update:value="getCode"></valid-code> <!--图形验证码-->
              </div>
            </div>

          </el-form-item>

          <el-form-item>
            <el-button type="primary" style="width: 100%" @click="login">登&nbsp; &nbsp;录</el-button>
          </el-form-item>
          <div style="display: flex">
            <div style="flex: 1">没有账号？请 <span style="color: darkcyan; cursor: pointer"
                                              @click="$router.push('/register')">注册</span></div>
            <div style="flex: 1; text-align: right; color: darkcyan">忘记密码</div>
          </div>

        </el-form>
      </div>
    </div>


  </div>
</template>


<script>

import ValidCode from "../components/ValidCode";

export default {
  name: "Login",
  components: {
    ValidCode
  },

  data() {

    //图形验证码校验
    const validCode = (rule, value, callback) => { //value是用户输入的图形验证码
      if (value === '') {
        callback(new Error('请输入验证码'));
      } else if (value.toLowerCase() !== this.code) {
        callback(new Error('验证码错误'));
      } else {
        callback(); //图形验证通过
      }
    };

    return {
      token:'', //登录的token——用户信息——(username,password,valid_code)
      //表单规定
      rules: {
        username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
        password: [{required: true, message: '请输入密码', trigger: 'blur'}],
        input_valid: {validator: validCode, trigger: 'blur'}
      },

      code: '',  //验证码组件里的code,通过getCode方法更新值

      user: {
        username: '',
        password: '',
        input_valid: '', //用户通过表单输入的图形验证码
        name: '',
        phone : '',
        email : '',
        address : '',
        role : '',
        avatar : ''
      },

      //存储在localStorage里面的user
    }

  },

  methods: {
    getCode(code) { //每次刷新图形，获得图形验证码，将其赋给code变量
      console.log(this.code)
      this.code = code.toLowerCase()
    },

    login() { //登录
      this.$refs['loginRef'].validate((valid) => {
        if (valid) {
          //验证通过
          //将登录数据存储到token中localStorage
          localStorage.setItem("token", JSON.stringify(this.user)) //参数为字符串类型
          console.log(JSON.parse(localStorage.getItem("token")).username) //将字符串转为JSON对象

          //user重复了吗？
          sessionStorage.setItem("user", this.user.username)

          //发送请求
          this.$request.post('/login', this.user).then(res => {
            //进入主界面
            if (res.code === '200') {

              this.$router.push('/')
              this.$message.success('登录成功！')
            } else {
              this.$message.error(res.msg)
            }
            console.log(res)
          })
        } else {
          //提示错误
        }
      });

    }
  }


}
</script>

<style scoped>

</style>